<?php
class Html extends A_Content 
{
	const 
		content_name = "HTML-блок";
	
		
	
	public function __get($param)
	{
		if ($param=="dir") {
			$html_path = $this->_top->_config->content->html_path;
			if ($html_path==null) {
				throw $this->_top->Utils->PushError('Html Store path not set. Check your ini file',"Системная ошибка. Обратитесь к разработчикам");
			}
			return $this->_top->_config->content->html_path;
		} elseif ($param=='content_name') {
			return self::content_name;
		} else { 
			return false;
		}
	}
	
	public function getById($html_id)
	{
		if (!isset($this->_cashe_obj[$html_id])) {
			$this->_cashe_obj[$html_id] = new SingleHtml($this->_top,$this,$html_id);
		}
		
		return $this->_cashe_obj[$html_id];
	}
	
	
	public function add()
	{
		$this->_top->_sql->exeDummy("INSERT INTO ".TAB_PREF."html(add_date,last_mod) VALUES(NOW(),NOW());");
		$html_id = $this->_top->_sql->last_insert_id();
		return $this->getById($html_id);
	}
	
}

class SingleHtml extends A_SingleContent 
{

	public function loader()
	{
		$get_data = $this->_top->_sql->exe("SELECT * FROM ".TAB_PREF."html c WHERE html_id={$this->_content_id}");
		if (count($get_data)==0)
			throw $this->_top->Utils->PushError("Html object Id: {$this->_content_id} not found", 'Системная ошибка. Обратитесь к разработчикам');
		else {
			$this->_data = $get_data[0];
		}
		return true;
	}
	
	public function __get($param)
	{
		if ($param=="data") {
			if (!isset($this->_data['data'])) {
				if ($this->file_ == '') { 
					return false;
				}
				$data_file = $this->_top->Html->dir . $this->file_;
				if (!file_exists($data_file) || is_dir($data_file)) {
					return false;
				}
				
				$this->_data['data'] = file_get_contents($data_file);
			}
			return $this->_data['data'];
		} elseif (!isset($this->_data[$param]))
			return false; 
		else 
			return $this->_data[$param];
	}
	
	public function __set($param,$value)
	{
		
		if ($param=="html_id")
			return false;
		elseif ($param=="add_date")
			return false;
		else {
			if ($param=="data") {
				
				if ($this->file_=="") { 
					$this->file_ = "{$this->_content_id}.html";
				}
				
				$data_file = $this->_parent->dir . $this->file_;
				// не плохо было бы проверочку на возможность записи в директорию и ексепшен
				$fp = fopen($data_file,"w+");
				fwrite($fp,$value);
				fclose($fp);
				
				$this->last_mod = true;
				return true;
			} elseif ($param=="last_mod") {
				$this->file_ = $this->file_;
				return true;
			} elseif (!isset($this->_data[$param])) {
				return false;
			}
			
			
			
			$this->_data[$param] = $value;
			$ins_value = $this->_top->Utils->prepareValueForSQL($value);
			$this->_top->_sql->exeDummy("UPDATE ".TAB_PREF."html SET `$param`='$ins_value',last_mod=NOW() WHERE html_id={$this->_content_id}");
			return true;
		}
			
	}
	
	public function deleter()
	{
		if ($this->file_!="") {
			$data_file = $this->_top->Html->dir . $this->file_;
			if (file_exists($data_file)) 
				unlink($data_file);
		}
		$this->_top->_sql->exeDummy("DELETE FROM ".TAB_PREF."html WHERE html_id={$this->_content_id}");
	}
}
?>